package com.tanhua.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.tanhua.admin.domain.AnalysisSummaryVo;

import com.tanhua.admin.service.AnalysisService;

import com.tanhua.model.Analysis;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private AnalysisService analysisService;

    /**
     * 概要统计信息
     */
    @GetMapping("/summary")
    public AnalysisSummaryVo getSummary() {

        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

        Date now = Calendar.getInstance().getTime();

        //累计用户数
        Integer total = analysisService.queryCumulativeUsers();
        analysisSummaryVo.setCumulativeUsers(Long.valueOf(total));

        //查询今日统计信息
        Analysis today_analysis = analysisService.querySummary(now);

        //过去30天活跃用户 TODO
        Integer thirtyDays = analysisService.querySummary30days();
        analysisSummaryVo.setActivePassMonth(Long.valueOf(thirtyDays));

        //过去7天活跃用户 TODO
        Integer Sdays= analysisService.querySummary7days();
        analysisSummaryVo.setActivePassWeek(Long.valueOf(Sdays));

        //今日活跃用户
        analysisSummaryVo.setActiveUsersToday(Long.valueOf(today_analysis.getNumActive()));

        //今日新增用户
        analysisSummaryVo.setNewUsersToday(Long.valueOf(today_analysis.getNumRegistered()));

        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        //查询昨日统计信息
        Analysis yes_analysis = analysisService.querySummary(DateUtils.addDays(now, -1));

        analysisSummaryVo.setNewUsersTodayRate(computeRate(Long.valueOf(today_analysis.getNumRegistered()), Long.valueOf(yes_analysis.getNumRegistered())));

        //今日登录次数
        analysisSummaryVo.setLoginTimesToday(Long.valueOf(today_analysis.getNumLogin()));

        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setLoginTimesTodayRate(computeRate(Long.valueOf(today_analysis.getNumLogin()), Long.valueOf(yes_analysis.getNumLogin())));

        //活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setActiveUsersTodayRate(computeRate(Long.valueOf(today_analysis.getNumActive()), Long.valueOf(yes_analysis.getNumActive())));

        return analysisSummaryVo;
    }

    @GetMapping("/users")
    public ResponseEntity users(@RequestParam("type") String type,@RequestParam("sd")Long sd
            , @RequestParam("ed")Long ed) throws ParseException {
        //今年开始时间
        Date date = new Date();
        date.setTime(sd);
        String start = new SimpleDateFormat("yyyy-MM-dd").format(date);
        //今年结束时间
        Date date1 = new Date();
        date1.setTime(ed);
        String end  = new SimpleDateFormat("yyyy-MM-dd").format(date1);
        //去年开始时间
        Date laststart = org.apache.commons.lang.time.DateUtils.addYears(new Date(sd), -1);
        String laststart1 = new SimpleDateFormat("yyyy-MM-dd").format(laststart);
        //去年结束时间
        Date lastend = org.apache.commons.lang.time.DateUtils.addYears(new Date(ed), -1);
        String lastend1 = new SimpleDateFormat("yyyy-MM-dd").format(lastend);

        switch (type){
            case "101" ://新增
                Map save = analysisService.findDate(start,end,laststart1,lastend1);
                return ResponseEntity.ok(save);
            case "102" ://活跃用户
                Map active = analysisService.findActive(start,end,laststart1,lastend1);
                return ResponseEntity.ok(active);
            case "103" ://次日留存率
                Map remain = analysisService.findRemain(start,end,laststart1,lastend1);
                return ResponseEntity.ok(remain);
        }
        return ResponseEntity.ok(null);
    }



    private static BigDecimal computeRate(Long current, Long last) {
        BigDecimal result;
        if (last == 0) {
            // 当上一期计数为零时，此时环比增长为倍数增长 1000 20
            result = new BigDecimal((current - last) * 100);
        } else {
            result = BigDecimal.valueOf((current - last) * 100).divide(BigDecimal.valueOf(last), 2, BigDecimal.ROUND_HALF_DOWN);
        }
        return result;
    }

    private static String offsetDay(Date date, int offSet) {
        return DateUtil.offsetDay(date, offSet).toDateStr();
    }
}